﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'user_regvcode';
</cfscript>

<cffunction name="clearoldvcode" access="remote">
	<!--- 清除过期的验证码 --->
  <cfquery datasource="#DB#">
  SELECT *
  from user_regvcode
  where cuts < UNIX_TIMESTAMP()-1800;
  </cfquery>
</cffunction>  

<cffunction name="createvcode" access="remote" >
	<cfinvoke method="clearoldvcode" />
  <!--- 查询 --->
  <cfquery datasource="#DB#" name="checkinputcounts">
  select counts
  from user_regvcode
  where mobile = '#mobile#'
  </cfquery>
  <cfscript>
	vcode = RandRange(100000,999999);
	</cfscript>
  <cfif checkinputcounts.recordcount eq 0>
    <cfquery datasource="#DB#">
    insert into user_regvcode(mobile,vcode,cuts)
    value('#mobile#','#vcode#',UNIX_TIMESTAMP())
    </cfquery>
  <cfelse>
    <cfquery datasource="#DB#">
    update user_regvcode set
    vcode = '#vcode#',
    cuts = UNIX_TIMESTAMP()
    where mobile = '#mobile#'
    </cfquery>
  </cfif>

</cffunction>

<cffunction name="checkvcode" access="remote" >
	<!---验证码是否过期--->
	<cfquery datasource="#DB#" name="checkvcode">
  select *
  from #dbt#
  where mobile = '#mobile#' and cuts > UNIX_TIMESTAMP()-1800; 
  </cfquery>
</cffunction>

  
</cfcomponent>